package com.utility.project.common.utils;

import cn.hutool.core.thread.ExecutorBuilder;

import java.util.concurrent.*;

/**
 * 创建线程池
 * @author yangzhiguo
 * @date 2023/2/20 17:23
 * @return
 */
public class ProjThreadPool {
	private volatile static ProjThreadPool instance = null;
	private ThreadPoolExecutor executor;

	private ProjThreadPool() {
		if (null == executor) {
			executor = ExecutorBuilder.create().setCorePoolSize(100).setKeepAliveTime(120, TimeUnit.SECONDS)
					.setAllowCoreThreadTimeOut(true).setHandler(new ThreadPoolExecutor.DiscardOldestPolicy())
					.setWorkQueue(new LinkedBlockingQueue<>(2000)).build();
		}
	}

	public static ProjThreadPool getInstance() {
		if (instance == null) {
			synchronized (ProjThreadPool.class) {
				if (instance == null) {
					instance = new ProjThreadPool();
				}
			}

		}
		return instance;
	}

	public ExecutorService getExecutor() {
		return this.executor;
	}

	public void executor(Runnable runnable) {
		executor.execute(runnable);
	}

	public Future executor(Callable callable) {
		return executor.submit(callable);
	}

	public void shutdown() {
		executor.shutdown();
	}
}
